package com.education.teach;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.Toast;

import com.education.teach.bean.TabBean;
import com.education.teach.mvp.ui.fragment.HomeFragment;
import com.education.teach.mvp.ui.fragment.SchoolFragment;
import com.education.teach.mvp.ui.fragment.UserFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;

import butterknife.BindView;
import dn.easy.common.base.BasePagerAdapter;

public class MainActivity extends BaseActivity {
    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.mTabLayout)
    CommonTabLayout mTabLayout;

    private long exitTime = 0;

    private String[] mTitles = {"学业报告", "自主学习", "个人中心"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private int[] mIconUnselectIds = {R.mipmap.icon_index_test, R.mipmap.icon_index_report, R.mipmap.icon_index_my};
    private int[] mIconSelectIds = {R.mipmap.icon_index_test_, R.mipmap.icon_index_report_, R.mipmap.icon_index_my_};

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        RichText.initCacheDir(this);
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabBean(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        mFragments.add(HomeFragment.newInstance());
        mFragments.add(SchoolFragment.newInstance());
        mFragments.add(UserFragment.newInstance());
        BasePagerAdapter mAdapter = new BasePagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mViewpager.setAdapter(mAdapter);
        mViewpager.setOffscreenPageLimit(3);
        setData();
    }

    private void setData() {
        mTabLayout.setTabData(mTabEntities);
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewpager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout.setCurrentTab(position);
//                mTabLayout.setTextSelectColor(R.color.user_center_bottom_text);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else
                ArmsUtils.exitApp();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        RichText.clear(this);
        super.onDestroy();

    }
}
